﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DemoAjaxQuery.Models;

namespace DemoAjaxQuery.Controllers
{
    public class ProController : ApiController
    {
        private CATALOG_DBEntities db = new CATALOG_DBEntities();

        // GET api/Pro
        public IEnumerable<ProductData> GetProducts()
        {
            var products = db.Products.Include(p => p.Category)
                .Select(it => new ProductData
                {
                    Id = it.Id,
                    Name = it.Name,
                    Description = it.Description,
                    CategoryId = it.CategoryId,
                    CategoryName = it.Category.Name,
                });
            return products.AsEnumerable();
        }

        // GET api/Pro/5
        public IEnumerable<ProductData> GetProducts(int id)
        {
            var qry = from it in db.Products
                      where it.CategoryId == id
                      select new ProductData
                        {
                            Id = it.Id,
                            Name = it.Name,
                            Description = it.Description,
                            CategoryId = it.CategoryId,
                            CategoryName = it.Category.Name,
                        };

            return qry.AsEnumerable();
        }
        //public ProductData GetProduct(int id) {
        //    var product = db.Products.Find(id);
        //    if (product == null)
        //    {
        //        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        //    }

        //    var it = product;
        //    return new ProductData
        //    {
        //        Id = it.Id,
        //        Name = it.Name,
        //        Description = it.Description,
        //        CategoryId = it.CategoryId,
        //        CategoryName = it.Category.Name,
        //    };
        //}

        // PUT api/Pro/5
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != product.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Pro
        public HttpResponseMessage PostProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Pro/5
        public HttpResponseMessage DeleteProduct(int id)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Products.Remove(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, product);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}